package org.shj.spark.operator;

import java.util.Arrays;
import java.util.List;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.VoidFunction;

public class FilterOperator {

	public static void main(String[] args) {
		SparkConf conf =new SparkConf().setMaster("local[2]").setAppName("FilterOperator");
		JavaSparkContext sc = new JavaSparkContext(conf);
		
		List<Integer> list = Arrays.asList(1,2, 3, 4, 5);
		
		JavaRDD<Integer> numRDD = sc.parallelize(list);
		
		//filter 算子，里面的逻辑返回true，则保留该数据，如果返回False，则过滤掉
		//一般filter算子后面会接一个coalesce 算子
		JavaRDD<Integer> filterRDD = numRDD.filter(new Function<Integer, Boolean>(){
			private static final long serialVersionUID = 8163629219099269783L;

			public Boolean call(Integer v1) throws Exception {
				return v1 % 2 == 0;
			}
			
		});
		
		filterRDD.foreach(new VoidFunction<Integer>(){
			private static final long serialVersionUID = 172026980798646603L;

			@Override
			public void call(Integer t) throws Exception {
				System.out.println(t);
			}
			
		});
		
		sc.close();

	}

}
